package com.dba.example.gwt.client.gin;

import com.dba.example.gwt.client.beans.factories.CompanyAutoBeanFactory;
import com.dba.example.gwt.client.commands.ExampleCommandFactory;
import com.dba.example.gwt.client.commands.impl.CompanyGetCommand;
import com.dba.example.gwt.client.commands.impl.CompanyPageCommand;
import com.dba.example.gwt.client.commands.impl.CompanySaveCommand;
import com.dba.example.gwt.client.places.HomePlace;
import com.dba.example.gwt.client.views.AccountView;
import com.dba.example.gwt.client.views.CompanyEditView;
import com.dba.example.gwt.client.views.CompanyListView;
import com.dba.example.gwt.client.views.HomeView;
import com.dba.example.gwt.client.views.impl.AccountViewImpl;
import com.dba.example.gwt.client.views.impl.CompanyEditViewImpl;
import com.dba.example.gwt.client.views.impl.CompanyListViewImpl;
import com.dba.example.gwt.client.views.impl.HomeViewImpl;
import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.inject.client.assistedinject.GinFactoryModuleBuilder;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.Command;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;

public class ExampleGinModule extends AbstractGinModule {

	@Override
	protected void configure() {
		bind(HomeView.class).to(HomeViewImpl.class).in(Singleton.class);
		bind(AccountView.class).to(AccountViewImpl.class).in(Singleton.class);
		bind(CompanyListView.class).to(CompanyListViewImpl.class).in(Singleton.class);
		bind(CompanyEditView.class).to(CompanyEditViewImpl.class).in(Singleton.class);

		bind(ActivityMapper.class).to(AppActivityMapper.class).in(Singleton.class);
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);
		bind(PlaceHistoryMapper.class).to(AppPlaceHistoryMapper.class).in(Singleton.class);
		
		bind(CompanyAutoBeanFactory.class).in(Singleton.class);
		
		GinFactoryModuleBuilder ginFactoryModuleBuilder = new GinFactoryModuleBuilder();
		ginFactoryModuleBuilder.implement(Command.class, Names.named("CompanyPage"), CompanyPageCommand.class);
		ginFactoryModuleBuilder.implement(Command.class, Names.named("CompanyGet"), CompanyGetCommand.class);
		ginFactoryModuleBuilder.implement(Command.class, Names.named("CompanySave"), CompanySaveCommand.class);
		install(ginFactoryModuleBuilder.build(ExampleCommandFactory.class));
	}

	@Provides
	@Singleton
	public ActivityManager provideActivityManager(
			ActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	public PlaceHistoryHandler providePlaceHistoryHandler(
			PlaceHistoryMapper placeHistoryMapper,
			PlaceController placeController, EventBus eventBus) {
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(placeHistoryMapper);
		historyHandler.register(placeController, eventBus, new HomePlace(null));
		return historyHandler;
	}

	@Provides
	@Singleton
	public PlaceController providePlaceController(EventBus eventBus) {
		return new PlaceController(eventBus);
	}

}
